﻿using Telerik.Windows.Controls;
using TruckScale.Helpers;
using TruckScale.Models;

namespace TruckScale.ViewModels
{
  public class RegViewModel : ViewModelBase
  {
    public string Theme { get; set; } = Global._weighConfig["Theme"];
    public string MainFontSize { get; set; } = Global._weighConfig["MainFontSize"];
    public string MachineCode { get; set; } = Global._weighConfig["MachineCode"];
    public string RegCode { get; set; } = Global._weighConfig["RegCode"];
    public string InfoBar { get; set; }
    public DelegateCommand GetCodeCommand { get; private set; }
    public DelegateCommand RegCodeCommand { get; private set; }
    public RegViewModel()
    {
      // 如果 MachineCode 和 realMachineCode 不同，更新 MachineCode 并保存配置
      string realMachineCode = RegisterHelper.GetHdInfo();
      if (!string.Equals(MachineCode, realMachineCode))
      {
        MachineCode = realMachineCode;
        WeighConfigModel.UpdateConfig("MachineCode", MachineCode);
      }

      RegCodeCommand = new DelegateCommand(OnRegCodeExecuted, OnCanRegCodeExecuted);
    }

    private bool OnCanRegCodeExecuted(object obj)
    {
      if (Global._isReg)
      {
        return false;
      }
      return true;
    }

    private void OnRegCodeExecuted(object obj)
    {
      string realRegCode = RegisterHelper.Verify(RegisterHelper.GetHdInfo());
      if (RegCode == realRegCode)
      {
        WeighConfigModel.UpdateConfig("RegCode", RegCode);
        InfoBar = "注册成功";
      }
      else
      {
        InfoBar = "注册码不正确";
      }

    }
  }
}
